package cn.exrick.xboot.modules.activiti.controller.business;

import cn.exrick.xboot.base.XbootBaseController;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.activiti.entity.business.Workorder;
import cn.exrick.xboot.modules.activiti.service.business.WorkorderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;
import cn.exrick.xboot.common.utils.SecurityUtil;
import cn.exrick.xboot.modules.activiti.entity.ActBusiness;
import cn.exrick.xboot.modules.activiti.service.ActBusinessService;
import cn.hutool.core.util.StrUtil;

/**
 * @author lihuan
 */
@Slf4j
@RestController
@Api(description = "工单流转管理接口")
@RequestMapping("/xboot/workorder")
@Transactional
public class WorkorderController extends XbootBaseController<Workorder, Long> {

    @Autowired
    private WorkorderService workorderService;

    @Override
    public WorkorderService getService() {
        return workorderService;
    }

    @Autowired
    private ActBusinessService actBusinessService;

    @Autowired
    private SecurityUtil securityUtil;

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<Page<Workorder>> getByCondition(@ModelAttribute Workorder workorder,
                                                            @ModelAttribute SearchVo searchVo,
                                                            @ModelAttribute PageVo pageVo){

        Page<Workorder> page = workorderService.findByCondition(workorder, searchVo, PageUtil.initPage(pageVo));
        for(Workorder obj: page.getContent()){
            // 关联流程表 查询关联流程状态和结果等信息
            if(StrUtil.isNotBlank(obj.getActBusinessId())){
                ActBusiness actBusiness = actBusinessService.get(obj.getActBusinessId());
                obj.setStatus(actBusiness.getStatus());
                obj.setResult(actBusiness.getResult());
                obj.setApplyTime(actBusiness.getApplyTime());
                obj.setRouteName(actBusiness.getRouteName());
                obj.setProcInstId(actBusiness.getProcInstId());
            }
        }
        return new ResultUtil<Page<Workorder>>().setData(page);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "添加关联流程")
    public Result<Object> add(@ModelAttribute Workorder workorder){

        Workorder obj = workorderService.save(workorder);
        // 保存至我的申请业务
        String userId = securityUtil.getCurrUser().getId();
        ActBusiness actBusiness = new ActBusiness();
        actBusiness.setUserId(userId);
        // 记录关联业务表ID
        actBusiness.setTableId(obj.getId());
        ActBusiness a = actBusinessService.save(actBusiness);
        // 记录关联流程状态表ID
        obj.setActBusinessId(a.getId());
        workorderService.update(obj);
        return new ResultUtil<Object>().setSuccessMsg("操作成功");
    }
}
